package com.jzy.chatglmsdk18753goldn.test;

import ai.z.openapi.ZhipuAiClient;
import ai.z.openapi.service.model.*;

import java.util.Arrays;

public class GLMTest {
    public static void main(String[] args) {
        // 初始化客户端
        ZhipuAiClient client = ZhipuAiClient.builder()
                .apiKey("bb70cc6355ef46cdb890e7b5359cf9c2.qD9j4XV15Dwa1kD8")
                .build();

// 创建聊天完成请求
        ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
                .model("glm-4.5-flash")
                .messages(Arrays.asList(
                        ChatMessage.builder()
                                .role(ChatMessageRole.USER.value())
                                .content("作为一名营销专家，请为我的产品创作一个吸引人的口号")
                                .build(),
                        ChatMessage.builder()
                                .role(ChatMessageRole.ASSISTANT.value())
                                .content("当然，要创作一个吸引人的口号，请告诉我一些关于您产品的信息")
                                .build(),
                        ChatMessage.builder()
                                .role(ChatMessageRole.USER.value())
                                .content("智谱AI 开放平台")
                                .build()
                ))
                .stream(true)
                .build();

        // 发送请求
        ChatCompletionResponse response = client.chat().createChatCompletion(request);

        // 获取回复
        if (response.isSuccess()) {
            response.getFlowable().subscribe(
                    // Process streaming message data
                    data -> {
                        if (data.getChoices() != null && !data.getChoices().isEmpty()) {
                            Delta delta = data.getChoices().get(0).getDelta();
                            System.out.print(delta + "\n");
                        }},
                    // Process streaming response error
                    error -> System.err.println("\nStream error: " + error.getMessage()),
                    // Process streaming response completion event
                    () -> System.out.println("\nStreaming response completed")
            );
        } else {
            System.err.println("Error: " + response.getMsg());
        }
    }
}